// pages/aHome/aHome.js
var service_common = require("../../service/common.js");
var service_user = require("../../service/user.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomID:-1,
    openid:"",
    roomData:{},
    nav_list:['朋友圈素材','成员'],
    inds:0,
    page:1,
    dataList:[],
    offon:true,
    Members:[],
    meberPage:1,
    meberOffon:true,
    isShow:false,
    tc_userInfo:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.data != undefined && options.data!=0) {
      var optionss = JSON.parse(options.data)
      console.log(options)
      getApp().globalData.uniq_openid = optionss.uniq_openid
      wx.setStorageSync('token__', optionss.token)
      wx.setStorageSync('token_key', optionss.token_key)
      service_common.fetchData(
        service_common.getUrl("user.getUserMpInfo"), {
          openid: optionss.openid
        },
        (res) => {
          if (res.data.code == 0) {
            wx.setStorageSync('userInfo', res.data.data);
            getApp().globalData.userInfo = res.data.data
            getApp().globalData.openid = res.data.data.openid
            getApp().globalData.unionid = res.data.data.unionid
            this.setData({
              roomID: options.roomID,
              isShow: true
            })
            this.getroomInfo()
            this.getroomContent()
            this.getroomMembers()
          }
        },
        "GET"
      )
    }else{
      this.setData({
        roomID: options.id,
        isShow: true
      })
      this.getroomInfo()
      this.getroomContent()
      this.getroomMembers()
    }
   
  },
  login: function () {
    var that = this;
    service_user.login((res) => {
      if (res == false) {//未授权
        wx.hideLoading()
        this.setData({
          tc_userInfo: true
        })
      }else{
        this.getroomInfo()
        this.getroomContent()
        this.getroomMembers()
      }
    }, this.data.openid);

  },
  // 房间海报
  RoomHb:function(){
    wx.showLoading({
      title: '正在生成海报',
    })
    service_common.fetchData(
      service_common.getUrl("user.getHb"),
      {
        openid: getApp().globalData.userInfo.openid,
        roomid: this.data.roomID
      },
      (res) => {
        if (res.data.code == 0) {
          wx.hideLoading()
          wx.previewImage({
            urls: [res.data.data],
          })
        }
      },
      "GET"
    )
  },
  // 授权
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      this.setData({
        tc_userInfo: false
      })
      this.login()
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  // 加入
  addHome:function(){
    service_common.fetchData(
      service_common.getUrl("user.joinRoom"),
      {
        openid: getApp().globalData.userInfo.openid,
        id: this.data.roomID
      },
      (res) => {
        if (res.data.code == 0) {
          this.getroomInfo()
          this.getroomContent()
          this.getroomMembers()
        }
      },
      "GET"
    )
  },
  // 退出
  exitHome: function () {
    service_common.fetchData(
      service_common.getUrl("user.exitRoom"),
      {
        openid: getApp().globalData.userInfo.openid,
        id: this.data.roomID
      },
      (res) => {
        if (res.data.code == 0) {
          this.getroomInfo()
          this.getroomContent()
          this.getroomMembers()
        }
      },
      "GET"
    )
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isShow){
      this.getroomInfo()
      this.getroomContent()
      this.getroomMembers()
    }
  },
  getroomContent:function(){
    service_common.fetchData(
      service_common.getUrl("user.roomContent"),
      {
        openid: getApp().globalData.userInfo.openid,
        'type': this.data.roomID,
        keyword:'',
        page:this.data.page
      },
      (res) => {
        if (res.data.code == 0) {
          if (this.data.page==1){
            this.setData({
              dataList:res.data.data
            })
          }else{
            if(res.data.data.length==0){
              wx.showToast({
                title: '数据全部加载完成...',
              })
            }else{
              this.data.dataList = this.data.dataList.concat(res.data.data)
              this.setData({
                offon:true,
                dataList:this.data.dataList
              })
            }
          }
        }
      },
      "GET"
    )
  },
  // 前往审核页面
  gosetData:function(){
    wx.navigateTo({
      url: '/pages/aHomeSet/aHomeSet?id=' + this.data.roomID,
    })
  },
  // 设置主房间
  setDefaultRoom:function(){
    service_common.fetchData(
      service_common.getUrl("user.roomMembers"),
      {
        openid: getApp().globalData.userInfo.openid,
        type_id: this.data.roomID
      },
      (res) => {
        if (res.data.code == 0) {
          
        }
      },
      "GET"
    )
  },
  getroomMembers:function(){
    service_common.fetchData(
      service_common.getUrl("user.roomMembers"),
      {
        openid: getApp().globalData.userInfo.openid,
        id: this.data.roomID,
        keyword: '',
        page: this.data.meberPage
      },
      (res) => {
        if (res.data.code == 0) {
          if (this.data.meberPage == 1) {
            this.setData({
              Members: res.data.data
            })
          } else {
            if (res.data.data.length == 0) {
              wx.showToast({
                title: '数据全部加载完成...',
              })
            } else {
              this.data.Members = this.data.Members.concat(res.data.data)
              this.setData({
                meberOffon: true,
                Members: this.data.Members
              })
            }
          }
        }
      },
      "GET"
    )
  },
  // 下拉触底
  lower:function(){
      if (this.data.inds == 0){
        if (this.data.offon){
          this.setData({
            offon: false,
            page: this.data.page += 1
          })
          this.getroomContent()
        }
        
      }else{
        if (this.data.meberOffon){
          this.setData({
            meberOffon:false,
            meberPage: this.data.meberPage += 1
          })
          this.getroomMembers()
        }
        
      }
  },
  // 获取我房间的信息
  getroomInfo:function(){
    service_common.fetchData(
      service_common.getUrl("user.roomInfo"),
      {
        openid: getApp().globalData.userInfo.openid,
        id: this.data.roomID
      },
      (res) => {
        if (res.data.code == 0) {
          this.setData({
            roomData:res.data.data
          })
        }
      },
      "GET"
    )
  },
  // 切换选择
  span_act:function(e){
   this.setData({
     inds: e.currentTarget.dataset.index
   })
  },
  // 滑动选择
  swiperChange:function(e){
    this.setData({
      inds: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
// 预览图片
  browPhoto:function(e){
    const list=[];

    for (let i in e.currentTarget.dataset.arr){
      list.push(e.currentTarget.dataset.arr[i].src)
    }

    wx.previewImage({
      urls: list,
      current: e.currentTarget.dataset.url
    })
  },
  //前往app下载
  goApp:function(){
    wx.showModal({
      title: '提示',
      content: '需要前去下载APP',
      confirmText: "前往",
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/aApp/aApp',
          })
        }
      }
    })
  },
  // 前往发布新的朋友圈
  add_pyq:function(){
    wx.navigateTo({
      url: '/pages/addPyq/addPyq?roomID=' + this.data.roomID,
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // 设置转发的路径
    var share_path = 'pages/auth/auth?webUrl=aHome&zf_openid=' + getApp().globalData.userInfo.openid + "&roomID=" + this.data.roomID + "&r=" + Math.random(100000);
    return {
      title: "您的好友邀请您和他一起组队，房间ID:" + this.data.roomData.code,
      desc: '您的好友邀请您和他一起组队',
      path: share_path,
      success: () => {
        wx.showToast({
          title: '邀请已发送',
        })
      },
      fail: res => {
        wx.showToast({
          title: '领取失败',
          image: '/image/cancel.png',
          duration: 2000
        });
      }
    }
  }
})